package com.polaris.live.popup.party

import android.content.Context
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.databinding.PopupSeatSettingBinding
import com.polaris.live.translation.R.string
import com.polaris.live.ui.party.widget.seat.bean.BaseSeatBean
import com.polaris.live.viewmodel.party.PartyAnchorFragmentModel

/**
 *  座位管理
 * @author Created by 天晴 on 2024/2/27 19:18
 * @since 1.0.0
 **/
class SeatSettingPopup(
    context: Context,
    private val seatBean: BaseSeatBean,
    private val viewModel: PartyAnchorFragmentModel,
    private val homeOwnerId: Long,
    private val objectId: Long,
    private val onUpperSeatListener: (() -> Unit)? = null,
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupSeatSettingBinding

    override fun getImplLayoutId(): Int {

        return R.layout.popup_seat_setting
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupSeatSettingBinding.bind(popupImplView)

        val lp = mBinding.bottom.layoutParams as MarginLayoutParams
        lp.height = NavigationBarUtils.getNavigationBarHeight(context)
        mBinding.bottom.layoutParams = lp

        val lock = seatBean.siteLock ?: false

        mBinding.lockTv.text =
            if (lock) StringUtils.getString(string.position_unlocking)
            else StringUtils.getString(string.position_locking)

        mBinding.invitingSeat.setOnSingleClickListener {
            showSeatPopup()
        }
        mBinding.positionLocking.setOnSingleClickListener {
            dismiss()
            viewModel.siteLock(seatBean.seatId, !lock, homeOwnerId)
        }
        mBinding.close.setOnSingleClickListener {
            dismiss()
        }

//        if (homeOwnerId == UserManager.getUserId()) {
//            mBinding.tvUpperSeat.visibility = View.GONE
//        }

        mBinding.tvUpperSeat.setOnSingleClickListener {
            onUpperSeatListener?.invoke()
            dismiss()
        }

    }

    /**
     * 显示座位管理
     */
    private fun showSeatPopup() {
        XPopup.Builder(context)
            .isViewMode(true)
            .hasShadowBg(true)
            .isDestroyOnDismiss(true)
            .asCustom(InvitingSeatListPopup(context, seatBean.seatId, viewModel, objectId))
            .show()
    }
}